package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


//游记
@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommentService travelCommentService;

    //查询页面数据
    @GetMapping("/query")
    public Object query(TravelQuery qo) {
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }


    //富文本上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


    //保存游记发表
    @PostMapping("/saveOrUpdate")
    @RequireLogin
    public Object saveOrUpdate(Travel travel,HttpServletRequest request) {
        travelService.saveOrUpdate(travel,request);
        return JsonResult.success(travel.getDestId());
    }



    //游记内容,编辑回显
    @GetMapping("/detail")
    public Object detail(String id) {
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }


    //测试参数解析器
    @GetMapping("/UserInfoArgumentResolver")
    public Object UserInfoArgumentResolver(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }


    //游记评论
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment,@UserParam UserInfo userInfo) {
        //使用Bean工具类复制对应的数据
        BeanUtils.copyProperties(userInfo,travelComment);
        //手动设置缺少的UserId和创建时间
        travelComment.setUserId(userInfo.getId());
        travelComment.setCreateTime(new Date());
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }


    //游记评论页面
    @GetMapping("/comments")
    public Object detail(TravelCommentQuery qo) {
        List<TravelComment> commentList = travelCommentService.querytravelCommentBytravelId(qo);
        return JsonResult.success(commentList);
    }
}

